﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;
using Sharp.Web.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

namespace Sharp.Web.Controllers
{
    [AutomaticTemplate]
    public class GroupController : Controller
    {
        // GET: Manage/Group
        public ActionResult Index(string containerId)
        {
            return new Ext.Net.MVC.PartialViewResult
            {
                Model = new GroupIndexModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }
        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(StoreRequestParameters parameters)
        {
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetGroupPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }
        // GET: Manage/Group/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            GroupDTO dto = null;
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetGroupInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            var viewModel = dto.To<GroupModel>();
            viewModel.OrgTree = OrgController.OrgTree();

            return new Ext.Net.MVC.PartialViewResult { Model = viewModel };
        }

        // GET: Manage/Group/Create
        public ActionResult Create()
        {
            return new Ext.Net.MVC.PartialViewResult
            {
                ViewName = "Create",
                Model = new GroupModel()
                {
                    OrgTree = OrgController.OrgTree()
                }
            };
        }


        // GET: Manage/Group/Edit/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            GroupDTO dto = null;
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetGroupInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            var viewModel = dto.To<GroupModel>();
            viewModel.OrgTree = OrgController.OrgTree();


            return new PartialViewResult { Model = viewModel };
        }


        [HttpPost]
        public ActionResult Save(GroupModel group)
        {
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                var dto = group.To<GroupDTO>();

                proxy.Channel.SaveGroup(dto);
            }

            return this.FormPanel();
        }

        [HttpPost]
        public JsonResult Delete(Guid[] ids)
        {
            var res = new JsonResult();
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                res.Data = proxy.Channel.DeleteGroups(ids);
            }

            return res;
        }

        
    }
}
